Page 1 of 1

Just starting out.

Posted: Thu Feb 01, 2007 9:37 pm
by Stangguy
arborint | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hey guys I am just starting out with PHP and MySQL. I have preious knowledge with programming and database programming so I wanted to jump right into this. I am testing this out on my computer which is a windows box running Appache (2.something the newest one) and PHP5. I also have MySQL running. I wrote some code using an Access DB and setting up an ODBC for it. Now I am trying to use MySQL and no matter what code I write I get a blank screen. I get no error messages or anything. I have some basic code that I will post in here. I'm not sure everything is set up properly because it seems to be doing absolutly nothing. Here is some basic code, and the DB is setup properly.

[syntax="php"]<?php 

$mysql_link = mysql_conn1ect("localhost","root","qwerty");
mysql_select_db("han", $mysql_link);
$sql = "SELECT * FROM Products";
$result = mysql_query( $sql );
$num=mysql_numrows($result);


while ($result_row = mysql_fetch_row(($result)))
{
	echo $result_row;

       echo 'Title: '.$result_row[1] . '<br />';
       echo 'Author: '.$result_row[4] . '<br /> ';  



}




?>

Like I said nothing gets displayed on the screen. Any ideas?


arborint | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Feb 02, 2007 2:59 am
by mikeq
Maybe include your variable $mysql_link in the function mysql_query($sql,$mysql_link).

I dont know why no one uses the function mysql_errno() or mysql_error()

Code: Select all

<?php 

$mysql_link = mysql_conn1ect("localhost","root","qwerty"); 
mysql_select_db("han", $mysql_link); 
$sql = "SELECT * FROM Products"; 
$result = mysql_query( $sql ); 
$num=mysql_numrows($result); 

//mysql_errno will return one of the mysql error codes if an error occurs, else it will be 0 and evaluate to false
if (mysql_errno()){
     $ErrorMessage =  "Error Code: ".mysql_errno()." Message: ".mysql_error()."<br />";
     error_log($ErrorMessage); //this will enter the error into the webserver error log
     print $ErrorMessage;
     exit;
}

while ($result_row = mysql_fetch_row(($result))) 
{ 
        echo $result_row; 

       echo 'Title: '.$result_row[1] . '<br />'; 
       echo 'Author: '.$result_row[4] . '<br /> ';  



} 

?>
Or alternatively as you are using PHP5 why not use exceptions

Code: Select all

<?php 

$mysql_link = mysql_conn1ect("localhost","root","qwerty"); 
mysql_select_db("han", $mysql_link); 
$sql = "SELECT * FROM Products"; 

try{
     $result = mysql_query( $sql ); 
     $num=mysql_numrows($result); 

     if (mysql_errno()){
          throw new Exception("Error Code: ".mysql_errno()." Message: ".mysql_error());
     }

     while ($result_row = mysql_fetch_row(($result))) 
     { 
        echo $result_row; 

       echo 'Title: '.$result_row[1] . '<br />'; 
       echo 'Author: '.$result_row[4] . '<br /> ';  

     } 
}
catch(Exception $e){
     error_log($e->getMessage());
     error_log($e->getTraceAsString());
}


Posted: Fri Feb 02, 2007 3:16 am
by mikeq
You might also want to look at using mysql_fetch_array() rather than mysql_fetch_row(), mysql_fetch_array() isnt that much slower but you get the benefit of being able to use an associative array. Personally I think this makes code easier to read, plus if the order of your columns change in the underlying table do you really want to re-write your code

so you would be able to use

Code: Select all

$result_row['title'];
$result_row['author'];
You would still have the option to use the column index as mysql_fetch_array returns both.

As you are new to PHP I dont know if your are aware that you can include variables when printing out to the screen if you use double quotes without the need to concatenate

so you can do

Code: Select all

print "this is my $Variable"; 
will put in the value of the variable. If the variable is an array it needs to be slightly different, just surround it with {}

Code: Select all

print "this is my array variable {$TheArray['thekey']}";
This is just my preference, 'I find'.it easier.' to read'.' without lots of '.'dots and quotes'

Posted: Fri Feb 02, 2007 9:13 am
by onion2k
I've never heard of the mysql_conn1ect() function.

Posted: Fri Feb 02, 2007 11:23 am
by Stangguy
Well I went and tryied all the different code you guys sugested and I still get a blank screen. I also took out that 1 in connect! Still though I get a blank screen with no error messages or anything. I wonder if MySql isn't set up properly?

Posted: Fri Feb 02, 2007 2:45 pm
by onion2k
I've not heard of mysql_numrows() either. mysql_num_rows() on the other hand...

I think your problem is that you need to turn on error reporting in your PHP configuration. Open up php.ini, find the error reporting value and change it to "error_reporting = E_ALL & ~E_NOTICE", then change display_errors to "display_errors = On".

Posted: Fri Feb 02, 2007 7:58 pm
by Stangguy
I added changed my php.ini file so I would get error message, but still I get a blank screen! I uploaded my file to some webspace I have that supports PHP but not MySQL and I got a ton of error messages on the screen! When I run the same file on my local computer I get nothing! PHP is set up correctly because when I run phpinfo() I get a screen full of info. What do I get a blank screen with no error message?

Posted: Sat Feb 03, 2007 12:49 pm
by califdon
I'd start by making sure Apache is running properly; can you see plain html pages on localhost? If it is, then I'd see if it is configured properly to run PHP; oh, you said it shows phpinfo(), so I guess both the above are okay.

So then you want to determine if it can connect to MySQL. Do something like this:

Code: Select all

<?php
mysql_connect("localhost","root","qwerty") or die("Couldn't connect to db");
mysql_select_db("han") or die("Couldn't select db");
$sql = "SELECT * FROM Products";
$result = mysql_query( $sql ) or die("Select query failed");
$row = mysql_fetch_assoc($result);
if (!$row) echo "No data"
  else
while ($row) {
  extract($row);
  echo $Title . "&nbsp;" . $Author . "<br />";
  $row = mysql_fetch_assoc($result);
}

Posted: Sat Feb 03, 2007 2:52 pm
by Ollie Saunders
Test your server by running

Code: Select all

<?php
phpinfo();
You may need to enable support for MySQL or use the MySQLI extension instead as you are using PHP5.

Posted: Tue Feb 06, 2007 9:24 pm
by Stangguy
Hey Guys,

Thanks for all your replies. Turns out all my problems was related to my install. Now that I have some good web hosting my connection to MySql actually works. I do have a question though. I am using a while loop to get all my rows from my DB. But it is diplaying all my rows except the very first one. Any ideas?

Code: Select all

while ($result_row = mysql_fetch_row($result)){
       echo $result_row[0] . '<br />';
       echo $result_row[1] . '<br />';
       echo $result_row[2] . '<br />';
}

Posted: Tue Feb 06, 2007 9:27 pm
by feyd
You are likely prematurely fetching a row before using it.