473,386 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Connecting to mysql with php

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>';
}
?>
Feb 14 '06 #1
11 1439
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
}

Feb 14 '06 #2
Following on from Howard's message. . .
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
T_STRING ... [FX whirr of brain cogs ] ... Check your quotes.

Use a highlighting editor.
$query = select * from books where ".$searchtype." like '%".$searchterm."%'
" ;


--
PETER FOX Not the same since the borehole business dried up
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Feb 14 '06 #3
>>$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!

--
Dave -
www.blurredistinction.com
http://www.macromedia.com/support/fo...am_macromedia/
Feb 14 '06 #4
Dave Mennenoh wrote:
$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!


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-

Feb 14 '06 #5
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" <da***********@sympatico.ca> wrote in message
news:LX*******************@fe29.usenetserver.com.. .
Dave Mennenoh wrote:
$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!


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-

Feb 14 '06 #6
Howard wrote:
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" <da***********@sympatico.ca> wrote in message
news:LX*******************@fe29.usenetserver.com.. .
Dave Mennenoh wrote:
> $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!

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-


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-

Feb 14 '06 #7
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.

Feb 15 '06 #8
Howard wrote:
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" <da***********@sympatico.ca> wrote in message
news:LX*******************@fe29.usenetserver.com.. .
Dave Mennenoh wrote:
>$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!


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-



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.
js*******@attglobal.net
==================
Feb 15 '06 #9
PHP reports to the Web Server error logs.

Feb 15 '06 #10
jonathan.beckett wrote:
PHP reports to the Web Server error logs.


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.
js*******@attglobal.net
==================
Feb 15 '06 #11
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" <gb******@databasix.com> wrote in message
news:ds**********@blackhelicopter.databasix.com...
On Tue, 14 Feb 2006 16:44:19 GMT, "Dave Mennenoh"
<da**@blurredistinction.com.invalid> wrote:
$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%'" ;


You have a mismatched double quote. :)
weird huh!


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
================================================== =========================

Feb 17 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Google Mike | last post by:
After a lot of thought and research, and playing with FreeTDS and InlineTDS, as well as various ODBC connections, I have determined that the fastest and cheapest way to get up and going with PHP on...
3
by: kamilla | last post by:
I have a mysql 3.5 server installed on a suse linux 8.1, with address 10.0.0.100. Now I want to access that db from a W2K pc, address 10.0.0.200. I am able to ping 10.0.0.100, but I cannot connect...
2
by: news | last post by:
We currently have our mySQL server on the same box as the Apache server. For security and load balancing, we're going to be moving the mySQL server to another box. We're already using a single...
4
by: CodeImp | last post by:
A simple app I quickly wrote to try getting info from a database. Here is the first part of its code. The rest of the code is irellevant. using System; using System.Data; using...
4
by: sympatico | last post by:
Hello Im seeking some code or way to connect VB.NET application with MySQL database. There are some softwares available which are very expensive. So anyone who knows to connect without a...
3
by: Jeremy Dillinger | last post by:
I am trying to design a program that will use data from a MySQL database. Currently all the data is being used with PHP scripts from a website. I am also trying to build a software solution that...
7
by: Frances | last post by:
this seems pretty straight-forward.. got this code $link = mysql_connect('localhost:3306', '<uid>', '<pswd>'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected...
3
by: Paradox Synthesist | last post by:
hi, i am a newbie to asp.net and have started learning mainly from websites. i have a question which IS very silly and trivial. ...
5
by: Ananthu | last post by:
Hi I have done all the codings part for connecting mysql server with java application but when i try to compile,the compilation is successful and during execution i get the following message, ...
8
by: Ananthu | last post by:
Hi I have done all the codings part for connecting mysql with java in eclipse environment. Coding Part: import java.sql.Connection; import java.sql.DriverManager; public class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.