a bit of database help

I have your basic database code here so I can pull the data to a web page. What I am asking/needing is My database has 31 columns. I am assuming ill have to type out all 31 diff column names, via a echo command like so " echo "

bid_id Bid_category
" there is no way to just tell it in code to say read the first column name through the last column name ? Code as follows not 100% done but I hope I am going in the right direction

[php]

display data from DB table { Border:2px solid red; background-color: #ffc; } th { border-bottom: 5px solid #000; } th { border bottom 2px solid #666; }

display data from DB

<?php mysql_connect(' http://www.biducation.com/phpMyAdmin/', 'username', 'password')or die(mysql_error()); echo "connected to MySQL

"; mysql_select_db("biducation")or die(mysql_error()); echo "connected to database

"; $query = "SELECT * FROM tbl_bids"; $result = mysql_query($query) or die(mysql_error()); echo "

"; echo " } ?> [/php]
bid_id Bid_category

Well, this is how I get data from a DB:

[php]

<?php $sql = mysql_query("SELECT * FROM tblbits"); while($result = mysql_fetch_array($sql)) { $id = $result['table_id']; $name = $result['table fields here']; // echo your regular code here // this code will loop through w/e you're getting from the DB. hope this helps. } ?>

[/php]

I will lol so hard at people giving other people advise on database scripts, especially knowing that mysql_query is going to be dropped from PHP

If you don’t keep up-to-date with modern standards, you’re destined to fail. You will wake up one day to find none of your scripts working because PHP has been upgraded on the server and you didn’t know. You will then have to rewrite all of your scripts as quickly and frantically as possible!

As per your question, here’s the proper way to do it:

dbconnect.php
[php]
//Connect to the database and test the connection

//Set variables
$db_myHost = “localhost”;
$db_myDatabase = “database_name”;
$db_myUser = “myUserName”;

//Specify connection
$dbconn = new PDO(‘mssql:host=’.$db_myHost.’;dbname=’.$db_myDatabase, $db_myUser, $db_myPassword);

//Test the connection
try
{
$dbPDO = new PDO(‘mssql:host=’.$db_myHost.’;dbname=’.$db_myDatabase, $db_myUser, $db_myPassword);
$dbPDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
//There was an error
catch (PDOException $e)
{
//echo "Error!: " . $e->getMessage() . "

//Kill the script
die();

}
[/php]

yourpage.php
[php]

Your Site

Hello!

<?php //Prepare the sql query //Note don't use * there's no point, only select what you need $sth = $dbconn->prepare(" SELECT username FROM users WHERE id = :id ");

//Set id and execute the query. I set the ID to 1 but you could do other things like grab $_GET[‘id’] from the URL
$sth->execute(array(
“:id” => ‘1’
));

$rows = $sth->fetchAll();

foreach ($rows as $row){
echo $row[‘table_id’];
}
?>

[/php]

I use PDO, but you can use MySQLi I guess it’s just preference. I also use MSSQL not MySQL but the code above works on both.

PDO and MySQLi automatically prevent SQL injections so it’s very important to use them if not for the integrity of your data then for the security of your website and/or it’s users.

Amen to that!! ;D 8)

Sponsor our Newsletter | Privacy Policy | Terms of Service