problem connecting to mysql from php

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
bm1
Forum Newbie
Posts: 7
Joined: Thu Feb 19, 2004 1:31 am
Location: nz

problem connecting to mysql from php

Post by bm1 »

hi im trying to connect to mysql with a php script and the result is i get nothing on the result page but the <h1>headings<h1>. i know ive used root but this is only for testing purposes.

Code: Select all

<?php

trim($firstName); 
trim($lastName); 
trim($age); 

if(!$firstName || !$lastName || !$age) { 
echo "you did not enter all the values<br>"; 
exit; 
} 

$firstName = addslashes($firstName); 
$lastName = addslashes($lastName); 
$age = intval($age); 

@ $db = msql_pconnect("localhost", "root", "password"); 
if(!$db) { 
echo "Error: could not connect to the msql database<br>"; 
exit; 
} 

echo "connection to the database was a success<br>"; 



?>
could this be a problem with how i have configured apache, php or mysql. any help would be much appreciated.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Topic says mysql but you've used msql_pconnect ... typo?
bm1
Forum Newbie
Posts: 7
Joined: Thu Feb 19, 2004 1:31 am
Location: nz

Post by bm1 »

i get the same result if i use mysql_connect, im told that mysql_pconnect is the same thing except that when you open a connection to a mysql server the connection doesnt shut when your script finishes. so then if you have to make a connection again you already have one. i think the p stands for persistant. or something like that. im new to php.
User avatar
Dr Evil
Forum Contributor
Posts: 184
Joined: Wed Jan 14, 2004 9:56 am
Location: Switzerland

Post by Dr Evil »

markl999 means :
mysql_pconnect instead of msql_pconnect


But then
Should the connection not be ?:

Code: Select all

<?php
$db = @mysql_pconnect("localhost", "root", "password");
?>
bm1
Forum Newbie
Posts: 7
Joined: Thu Feb 19, 2004 1:31 am
Location: nz

Post by bm1 »

yeah sorry about the typo, i have got this working with the help of a friend using

Code: Select all

<?php

 $connection = mysql_connect("localhost", "uniforms", "password") or die(mysql_error());

?>
now this code worked with the

Code: Select all

<?php

or die(mysql_error());

?>
and i dont know why? iam reading thru a book and it doesnt use the or die. help me understand why please
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

the or die is just for tesitng purposes, it will display the error (if any at all) so you know how to correct the code.


connect to db OR display error in generic terms
bm1
Forum Newbie
Posts: 7
Joined: Thu Feb 19, 2004 1:31 am
Location: nz

Post by bm1 »

thanks for the help. i got it working now which is very cool
Post Reply