|
-
Jul 24th, 2005, 08:27 PM
#1
Thread Starter
Addicted Member
about PHP and mysql
sup? all
i don't know anything about php, but i was wondering if anyone have sample example how to read mysql Database, lets say when a user type in a link like this:
http://www.hostname.com/Serial.php?123456
123456 is the serial in the mysql database, once the serial match in the database it will show a sample with the serial on it.
can anyone help me out here i'm been searching for this for awhile
Thankx
-
Jul 24th, 2005, 08:35 PM
#2
Re: about PHP and mysql
http://www.hostname.com/Serial.php?123456
hmmm.... this particular construct... 123456... doesn't follow query string contruct.. now if you have this like this
http://www.hostname.com/Serial.php?serial=123456
then you can get 123456 by
$_GET["serial"] in your php code...
and from there... create your sql statement. 
then pass it to something like
mysql_query($strQUERY); something like that.. I havn't used mysql routines... I work mostly using pgsql.. by I should say they are somewhat the same
-
Jul 24th, 2005, 09:02 PM
#3
Thread Starter
Addicted Member
Re: about PHP and mysql
 Originally Posted by oceanebelle
http://www.hostname.com/Serial.php?123456
hmmm.... this particular construct... 123456... doesn't follow query string contruct.. now if you have this like this
http://www.hostname.com/Serial.php?serial=123456
then you can get 123456 by
$_GET["serial"] in your php code...
and from there... create your sql statement. 
then pass it to something like
mysql_query($strQUERY); something like that.. I havn't used mysql routines... I work mostly using pgsql.. by I should say they are somewhat the same 
yeah that is what i meant , like i said i'm don't know anything about php
is there a chance you can make a lil example like that with connect to mysql?
Thankx so much
-
Jul 24th, 2005, 09:32 PM
#4
Re: about PHP and mysql
assuming this code is in serial.php
PHP Code:
<?php
$link = mysql_connect('hostname', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
var $nSerial = $_GET["serial"];
var $strQUERY = "SELECT * FROM mydatabase.mytable where serial =";
var $result;
if ($nSerial != ""){
$strQUERY = $strQUERY + "'"+ trim($nSerial) + "'";
$result = mysql_query($strQUERY, $link);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
if(mysql_num_rows($result) > 0){
echo 'serial number is found';
}
// do code here to continue processing whatever you want to process. :D
} else {
echo 'Serial not found';
}
mysql_close($link);
?>
I basically copied bits of code from dox... and I have not tested this because I don't have mysql with me... but I'd say this is ONE of the ways to do this. 
Hope this helps
-oceane
-
Jul 24th, 2005, 10:12 PM
#5
Thread Starter
Addicted Member
Re: about PHP and mysql
here is what i had to view the database so what do i have to change the code u had?
PHP Code:
<?php
$link = mysql_pconnect("localhost", "root", "123456");
mysql_select_db("serial") or die("Bad DataBase");
$query ="SELECT * FROM products";
$result = mysql_query($query) or die ("Bad Send" . mysql_error( ));
while ( $row = mysql_fetch_array($result) ) {
echo "<b>UserName: </b>".$row['Name']."<br>";
echo "<b>Date: </b>".$row['Date']."<br>";
echo "<b>E-mail: </b>".$row['eMail']."<br>";
echo "<b>Serial: </b>".$row['Serial']."<br>";
}
mysql_free_result($result);
-
Jul 24th, 2005, 10:29 PM
#6
Re: about PHP and mysql
 Originally Posted by lmf
here is what i had to view the database so what do i have to change the code u had?
PHP Code:
<?php
$link = mysql_pconnect("localhost", "root", "123456");
mysql_select_db("serial") or die("Bad DataBase");
$query ="SELECT * FROM products";
$result = mysql_query($query) or die ("Bad Send" . mysql_error( ));
while ( $row = mysql_fetch_array($result) ) {
echo "<b>UserName: </b>".$row['Name']."<br>";
echo "<b>Date: </b>".$row['Date']."<br>";
echo "<b>E-mail: </b>".$row['eMail']."<br>";
echo "<b>Serial: </b>".$row['Serial']."<br>";
}
mysql_free_result($result);
use mysql_fetch_row instead of array... array returns all rows in one go.. and you wanted to loop through the records right? 
i have this in mind
PHP Code:
while ( $row = mysql_fetch_row($result) ) {
echo "<b>UserName: </b>".$row[0]."<br>";
echo "<b>Date: </b>".$row[1]."<br>";
echo "<b>E-mail: </b>".$row[2]."<br>";
echo "<b>Serial: </b>".$row[3]."<br>";
}
notice I used numbers for the index instead of name...
just make sure you are accessing the right field in that order. 
Hope this helps
-oceane
-
Jul 24th, 2005, 11:16 PM
#7
Re: about PHP and mysql
PHP Code:
<?php
$link = mysql_pconnect("localhost", "root", "123456");
mysql_select_db("products") or die("Bad DataBase");
$query ="SELECT Name, Date, eMail, sSerial FROM serial WHERE sSerial = '" + $_GET["serial"]+ "'";
$result = mysql_query($query) or die ("Bad Send" . mysql_error( ));
while ( $row = mysql_fetch_row($result) ) {
echo "<b>UserName: </b>".$row[0]."<br>";
echo "<b>Date: </b>".$row[1]."<br>";
echo "<b>E-mail: </b>".$row[2]."<br>";
echo "<b>Serial: </b>".$row[3]."<br>";
}
mysql_free_result($result);
mysql_close($link);
so according to what you told me...
products - database
serial -table
sSerial - column
Hope this one is clear.
-
Jul 28th, 2005, 05:16 AM
#8
Thread Starter
Addicted Member
Re: about PHP and mysql
 Originally Posted by oceanebelle
PHP Code:
<?php
$link = mysql_pconnect("localhost", "root", "123456");
mysql_select_db("products") or die("Bad DataBase");
$query ="SELECT Name, Date, eMail, sSerial FROM serial WHERE sSerial = '" + $_GET["serial"]+ "'";
$result = mysql_query($query) or die ("Bad Send" . mysql_error( ));
while ( $row = mysql_fetch_row($result) ) {
echo "<b>UserName: </b>".$row[0]."<br>";
echo "<b>Date: </b>".$row[1]."<br>";
echo "<b>E-mail: </b>".$row[2]."<br>";
echo "<b>Serial: </b>".$row[3]."<br>";
}
mysql_free_result($result);
mysql_close($link);
so according to what you told me...
products - database
serial -table
sSerial - column
Hope this one is clear. 
I was wondering if $result don't come up with anything, can i post a massage?
Thx
-
Jul 28th, 2005, 06:57 AM
#9
PowerPoster
Re: about PHP and mysql
http://au2.php.net/mysql You can find plenty of sample code here as well as function definitions.
PHP Code:
$result = mysql_query($query) or die ("Bad Send" . mysql_error( ));
if(!$result) {
while ( $row = mysql_fetch_row($result) ) {
echo "<b>UserName: </b>".$row[0]."<br>";
echo "<b>Date: </b>".$row[1]."<br>";
echo "<b>E-mail: </b>".$row[2]."<br>";
echo "<b>Serial: </b>".$row[3]."<br>";
}
} else {
//I'm more of a print man than an echo man really
print "No Records Found Noob";
}
If a query returns no results, then it returns false.
http://au2.php.net/mysql_query
Hopefully that code works.
-
Jul 29th, 2005, 06:21 AM
#10
Re: about PHP and mysql
 Originally Posted by Pc_Madness
http://au2.php.net/mysql You can find plenty of sample code here as well as function definitions.
PHP Code:
$result = mysql_query($query) or die ("Bad Send" . mysql_error( ));
if(!$result) {
while ( $row = mysql_fetch_row($result) ) {
echo "<b>UserName: </b>".$row[0]."<br>";
echo "<b>Date: </b>".$row[1]."<br>";
echo "<b>E-mail: </b>".$row[2]."<br>";
echo "<b>Serial: </b>".$row[3]."<br>";
}
} else {
//I'm more of a print man than an echo man really
print "No Records Found Noob";
}
If a query returns no results, then it returns false.
http://au2.php.net/mysql_query
Hopefully that code works. 
If thats the case, your snippet will say "No Records Found Noob" when there are, and possibly throw an error when there's none:
..should be?:
?
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Jul 29th, 2005, 06:29 AM
#11
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|