Page 1 of 1

Need help with my crappy code to get PHP to return some data

Posted: Wed Aug 18, 2010 9:35 pm
by prototype18
...from MySQL

Here is my code...

$sqlquery = "SELECT * FROM liecon63_bizdir WHERE bizcategory = '".$category."'";
$sqlresult = mysql_query($sqlquery);
$sqlmyrow = mysql_fetch_array($sqlresult, MYSQL_ASSOC);

but I'm getting errors on the third line...

Re: Need help with my crappy code to get PHP to return some

Posted: Wed Aug 18, 2010 9:47 pm
by califdon
prototype18 wrote:...from MySQL

Here is my code...

$sqlquery = "SELECT * FROM liecon63_bizdir WHERE bizcategory = '".$category."'";
$sqlresult = mysql_query($sqlquery);
$sqlmyrow = mysql_fetch_array($sqlresult, MYSQL_ASSOC);

but I'm getting errors on the third line...
What errors?

On all mysql_ commands, it is helpful to trap the MySQL server error message if there is one, by using or die(mysql_error()), which will have no effect if the command ends successfully, but will halt execution and print the MySQL server error message if it fails. Like this:

Code: Select all

$sqlresult = mysql_query($sqlquery) or die(mysql_error();
$sqlmyrow = mysql_fetch_array($sqlresult, MYSQL_ASSOC) or die(mysql_error();

Re: Need help with my crappy code to get PHP to return some

Posted: Thu Aug 19, 2010 6:12 am
by prototype18
Okay, I trappedthe mysql error message and its saying that there is no db selected...

Re: Need help with my crappy code to get PHP to return some

Posted: Thu Aug 19, 2010 6:16 am
by timWebUK
Ensure once you have made a connection to the database host, you select the correct database.

See here:

Code: Select all

<?php

//Database credentials
$dbHost = 'localhost';
$dbUser = 'username';
$dbPass = 'password';
$dbName = 'databasename';

//Connect to MySQL database server
$connection = mysql_connect($dbHost, $dbUser, $dbPass) or die(mysql_error());
		
//Select Database
mysql_select_db($dbName, $connection) or die(mysql_error()); 

?>