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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
prototype18
Forum Commoner
Posts: 25
Joined: Wed Aug 18, 2010 9:52 am

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

Post 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...
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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();
prototype18
Forum Commoner
Posts: 25
Joined: Wed Aug 18, 2010 9:52 am

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

Post by prototype18 »

Okay, I trappedthe mysql error message and its saying that there is no db selected...
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

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

Post 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()); 

?>
Post Reply