Connecting Tech Pros Worldwide Forums | Help | Site Map

Connecting to mysql with php

Howard
Guest
 
Posts: n/a
#1: Feb 14 '06
Hi,

I have been racking my brain trying to figure out what I am missing trying
to follow this book to get a php script to work. I would really appreciate
if someone could point out my errors. I've been up really late going over
all the config files. Why do they always scim installation for the
applications and put it in the Appendix when its the first thing that new
users need to do. Then we run into these problems because the author takes
so much for granted

Thanks

Howard

I'm using Apache 2.0.55 with PHP 5.1.2 and MySql AB 5.0.18. Windows XP SP2.

What I believe I'm missing is how do I tell PHP & Apache where the Databases
are??

Here is the pathing my my installs.
C:/mysql/bin, c:/mysql/data/books, c:/apached/apache2/htdocs for servering
up web pages, and c:/php.

Here is the error log in apache, and the script

[Tue Feb 14 03:31:27 2006] [error] [client 127.0.0.1] PHP Parse error:
syntax error, unexpected T_STRING in
C:\\Apache\\Apache2\\htdocs\\results.php on line 30, referer:
http://127.0.0.1/search.html

And here is the script. The line 30 is the $result = mysql_query if you
count starting with 1 or $query = select if the first line is 0.


<?php

include config.php;
include opendb.php;

// create short variable names

$searchtype=$_POST['searchtype'];
$searchterm=$_POST['searchterm'];
$searchterm= trim($searchterm);

if (!$searchtype || !$searchterm)

{
echo 'You have not entered search details. Please go back and try again.';
exit;
}

mysql_select_db('books');
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);

$query = select * from books where ".$searchtype." like '%".$searchterm."%'
" ;
$result = mysql_query($query);

$num_results = mysql_num_rows($result);

echo '<p>Number of books found: '.$num_results.'</p>';

for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result);
echo '<p><strong>'.($i+1).'. Title: ';
echo htmlspecialchars(stripslashes($row['title']));
echo '</strong><br />Author: ';
echo stripslashes($row['author']);
echo '<br />ISBN: ';
echo stripslashes($row['isbn']);
echo '<br />Price: ';
echo stripslashes($row['price']);
echo '</p>';
}
?>



jonathan.beckett
Guest
 
Posts: n/a
#2: Feb 14 '06

re: Connecting to mysql with php


Okay... for a start, you have a quote mark missing at the start of your
query.

Here's some boilerplate code that will come in handy...

// you can put these in a config file
$db_server = "locahost";
$db_name = "foo";
$db_username = "root";
$db_password = "";

// description : connects to database and returns handle
function db_connect(){

global $db_server;
global $db_name;
global $db_username;
global $db_password;

$con =@ mysql_connect($db_server,$db_username,$db_password );
if ($con!=false){
if (!(mysql_select_db($db_name,$con))){
// could not connect
}
}
return $con;
}


// how to do a connection...
$con = db_connect();
$sql = "SELECT foo FROM bar";
$result = mysql_query($sql,$con);
if ($result!=false){
if (mysql_num_rows($result)>0){
while ($row =@ mysql_fetch_array($result)){
// $row now holds an associative array of one row from your query
// i.e. do something with it
}
} else {
// no records returned
}
} else {
// problem with SQL
}

Peter Fox
Guest
 
Posts: n/a
#3: Feb 14 '06

re: Connecting to mysql with php


Following on from Howard's message. . .[color=blue]
>Hi,
>
>I have been racking my brain trying to figure out what I am missing trying
>to follow this book to get a php script to work. I would really appreciate
>if someone could point out my errors. I've been up really late going over
>all the config files. Why do they always scim installation for the
>applications and put it in the Appendix when its the first thing that new
>users need to do. Then we run into these problems because the author takes
>so much for granted[/color]

T_STRING ... [FX whirr of brain cogs ] ... Check your quotes.

Use a highlighting editor.
[color=blue]
>$query = select * from books where ".$searchtype." like '%".$searchterm."%'
>" ;[/color]

--
PETER FOX Not the same since the borehole business dried up
peterfox@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Dave Mennenoh
Guest
 
Posts: n/a
#4: Feb 14 '06

re: Connecting to mysql with php


>>$query = select * from books where ".$searchtype." like[color=blue][color=green]
>>'%".$searchterm."%'" ;[/color][/color]

Besides missing the leading quotation mark, you might like to know that you
don't need to do concatenation like this using PHP - so you can simply do
this:

$query = select * from books where $searchtype like '%$searchterm%'" ;

weird huh!

--
Dave -
www.blurredistinction.com
http://www.macromedia.com/support/fo...am_macromedia/


David Haynes
Guest
 
Posts: n/a
#5: Feb 14 '06

re: Connecting to mysql with php


Dave Mennenoh wrote:[color=blue][color=green][color=darkred]
>>> $query = select * from books where ".$searchtype." like
>>> '%".$searchterm."%'" ;[/color][/color]
>
> Besides missing the leading quotation mark, you might like to know that you
> don't need to do concatenation like this using PHP - so you can simply do
> this:
>
> $query = select * from books where $searchtype like '%$searchterm%'" ;
>
> weird huh!
>[/color]

You also might like to use a format like:

$query = <<<EOQ
SELECT *
FROM books
WHERE {$searchtype} LIKE '%{$searchterm}%'
EOQ;

Which make the whole query a lot clearer to those used to SQL CLI
environments.

-david-

Howard
Guest
 
Posts: n/a
#6: Feb 14 '06

re: Connecting to mysql with php


The script runs but there is no data. I get blank page.

I copied the info for making a config.php file.
So I guess I want to try using an include statment to call those files and
see if
that works.
The scripts were copied right from the CD-ROM so I don't know why the
echo statements don't seem to be doing anything.

I put a require ('config.php') in the script. Still nothing shows up and no
errors in
the apache log.

I have a feeling that it has to do with a configuration file with php or
mysql.

Thanks So far

Howard


"David Haynes" <david.haynes2@sympatico.ca> wrote in message
news:LXnIf.23728$Hj6.11243@fe29.usenetserver.com.. .[color=blue]
> Dave Mennenoh wrote:[color=green][color=darkred]
>>>> $query = select * from books where ".$searchtype." like
>>>> '%".$searchterm."%'" ;[/color]
>>
>> Besides missing the leading quotation mark, you might like to know that
>> you don't need to do concatenation like this using PHP - so you can
>> simply do this:
>>
>> $query = select * from books where $searchtype like '%$searchterm%'" ;
>>
>> weird huh!
>>[/color]
>
> You also might like to use a format like:
>
> $query = <<<EOQ
> SELECT *
> FROM books
> WHERE {$searchtype} LIKE '%{$searchterm}%'
> EOQ;
>
> Which make the whole query a lot clearer to those used to SQL CLI
> environments.
>
> -david-
>[/color]


David Haynes
Guest
 
Posts: n/a
#7: Feb 14 '06

re: Connecting to mysql with php


Howard wrote:[color=blue]
> The script runs but there is no data. I get blank page.
>
> I copied the info for making a config.php file.
> So I guess I want to try using an include statment to call those files and
> see if
> that works.
> The scripts were copied right from the CD-ROM so I don't know why the
> echo statements don't seem to be doing anything.
>
> I put a require ('config.php') in the script. Still nothing shows up and no
> errors in
> the apache log.
>
> I have a feeling that it has to do with a configuration file with php or
> mysql.
>
> Thanks So far
>
> Howard
>
>
> "David Haynes" <david.haynes2@sympatico.ca> wrote in message
> news:LXnIf.23728$Hj6.11243@fe29.usenetserver.com.. .[color=green]
>> Dave Mennenoh wrote:[color=darkred]
>>>>> $query = select * from books where ".$searchtype." like
>>>>> '%".$searchterm."%'" ;
>>> Besides missing the leading quotation mark, you might like to know that
>>> you don't need to do concatenation like this using PHP - so you can
>>> simply do this:
>>>
>>> $query = select * from books where $searchtype like '%$searchterm%'" ;
>>>
>>> weird huh!
>>>[/color]
>> You also might like to use a format like:
>>
>> $query = <<<EOQ
>> SELECT *
>> FROM books
>> WHERE {$searchtype} LIKE '%{$searchterm}%'
>> EOQ;
>>
>> Which make the whole query a lot clearer to those used to SQL CLI
>> environments.
>>
>> -david-[/color][/color]

Why not post snippets of your database connection code, your query
execution code and your html display code? It's very difficult to help
you with no examples...

-david-

Skeets
Guest
 
Posts: n/a
#8: Feb 15 '06

re: Connecting to mysql with php


howard, this is where debug skills come in. try something like

echo 'here';
die;

and place it within your script. if you see it, move it forward. if
you don't move it backwards until you see it. you can find the
location of the error.

i *highly* recommend the adodb db abstraction layer. i use it with
postgresql, but i would use it exactly the same with the mysql, too.
that's the point. ;-)

http://phplens.com/lens/adodb/docs-adodb.htm

you should also test to make sure you are connecting to the db. is a
recordset being produced? you need to know how to check for these
kinds of things.

adodb has db debug capability, too.

set

$db->debug = true;

it tells you if and where the deb chokes.

i never was able to get phpp error reporting when displaying php
(winxp, php-cgi), but i use and ide that is able to debug my php. this
*really* helps.

i also use a forms generation and validation class by manuel lemos - it
is great. it can be found on phpclasses.org.

this is no easy project, but if you go head first and keep moving your
feet, you will get somewhere. i know i did.

Jerry Stuckle
Guest
 
Posts: n/a
#9: Feb 15 '06

re: Connecting to mysql with php


Howard wrote:[color=blue]
> The script runs but there is no data. I get blank page.
>
> I copied the info for making a config.php file.
> So I guess I want to try using an include statment to call those files and
> see if
> that works.
> The scripts were copied right from the CD-ROM so I don't know why the
> echo statements don't seem to be doing anything.
>
> I put a require ('config.php') in the script. Still nothing shows up and no
> errors in
> the apache log.
>
> I have a feeling that it has to do with a configuration file with php or
> mysql.
>
> Thanks So far
>
> Howard
>
>
> "David Haynes" <david.haynes2@sympatico.ca> wrote in message
> news:LXnIf.23728$Hj6.11243@fe29.usenetserver.com.. .
>[color=green]
>>Dave Mennenoh wrote:
>>[color=darkred]
>>>>>$query = select * from books where ".$searchtype." like
>>>>>'%".$searchterm."%'" ;
>>>
>>>Besides missing the leading quotation mark, you might like to know that
>>>you don't need to do concatenation like this using PHP - so you can
>>>simply do this:
>>>
>>>$query = select * from books where $searchtype like '%$searchterm%'" ;
>>>
>>>weird huh!
>>>[/color]
>>
>>You also might like to use a format like:
>>
>>$query = <<<EOQ
>>SELECT *
>>FROM books
>>WHERE {$searchtype} LIKE '%{$searchterm}%'
>>EOQ;
>>
>>Which make the whole query a lot clearer to those used to SQL CLI
>>environments.
>>
>>-david-
>>[/color]
>
>
>[/color]

David,

How about your PHP error log? Is there anything in it?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
jonathan.beckett
Guest
 
Posts: n/a
#10: Feb 15 '06

re: Connecting to mysql with php


PHP reports to the Web Server error logs.

Jerry Stuckle
Guest
 
Posts: n/a
#11: Feb 15 '06

re: Connecting to mysql with php


jonathan.beckett wrote:[color=blue]
> PHP reports to the Web Server error logs.
>[/color]

Not necessarily. It can log to its own log file, also. It depends on
your configuration.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Howard
Guest
 
Posts: n/a
#12: Feb 17 '06

re: Connecting to mysql with php


Thanks for the Help to All,

All your code was a lot easier to follow than the books. A lot cleaner.
Begs the question why they wrote it that way??

So It's working and I'm even on to getting phpMyAdmin working to make life
easier.

Howard


"Gary L. Burnore" <gburnore@databasix.com> wrote in message
news:dstmk4$1mt$1@blackhelicopter.databasix.com...[color=blue]
> On Tue, 14 Feb 2006 16:44:19 GMT, "Dave Mennenoh"
> <dave@blurredistinction.com.invalid> wrote:
>[color=green][color=darkred]
>>>>$query = select * from books where ".$searchtype." like
>>>>'%".$searchterm."%'" ;[/color]
>>
>>Besides missing the leading quotation mark, you might like to know that
>>you
>>don't need to do concatenation like this using PHP - so you can simply do
>>this:
>>
>>$query = select * from books where $searchtype like '%$searchterm%'" ;[/color]
>
> You have a mismatched double quote. :)
>[color=green]
>>weird huh![/color]
>
> I always thought so.
> --
> gburnore at DataBasix dot Com
> ---------------------------------------------------------------------------
> How you look depends on where you go.
> ---------------------------------------------------------------------------
> Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
> | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
> Official .sig, Accept no substitutes. | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
> | ÝÛ 0 1 7 2 3 / Ý³Þ 3 7 4 9 3 0 Û³
> Black Helicopter Repair Services, Ltd.| Official Proof of Purchase
> ================================================== =========================[/color]


Closed Thread