 |
| PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 31st, 2008, 06:32 AM
|
|
Registered User
|
|
Join Date: Oct 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
what am I doing wrong?
I have setup a database called my_db, I have checked that it is there, in mysql.
I have the correct username and password (even tried with root, same lack of result) and I have tried "localhost" instead of the ip address too.
my server is, obviously, the localhost. I'm working in winxp, using the latest PHP and MySQL and running Apache (also the latest).
everything is connected well, I can even see and alter my databases using PHPmyAdmin and yet, everytime I try and execute a script against a database I get NO result. nothing. no errors or data returned.
I'm really stuck here for answers :|.
Please help.
my php code:
<?
$usr = "****";
$pwd = "****";
$host = "127.0.0.1:3306";
$db = "my_db";
# connect to database
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }
?>
<HTML>
<HEAD>
<TITLE>Insert projects</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<P><B> Add project </B> </P>
<?
# this is processed when the form is submitted
# back on to this page (POST METHOD)
if ($REQUEST_METHOD=="POST") {
# double-up apostrophes
$description = str_replace("'","''",$description);
$sitename = str_replace("'","''",$sitename);
# setup SQL statement
$SQL = " INSERT INTO projects ";
$SQL = $SQL . " (category, sitename, siteURL, description) VALUES ";
$SQL = $SQL . " ('$category', '$sitename','$siteurl','$description') ";
#execute SQL statement
$result = mysql_db_query($db,"$SQL",$cid);
# check for error
if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); }
echo ("<P><B>New Projects Added</B></P>\n");
}
?>
<FORM NAME="fa" ACTION="insert_link.php" METHOD="POST">
<TABLE>
<TR><TD><B>Category: </B> </TD><TD><INPUT TYPE="text" NAME="category" SIZE=40></TD></TR>
<TR><TD><B>Site Name:</B> </TD><TD><INPUT TYPE="text" NAME="sitename" SIZE=40></TD></TR>
<TR><TD><B>Site URL: </B> </TD><TD><INPUT TYPE="text" NAME="siteurl" VALUE="http://" SIZE=40></TD></TR>
<TR><TD VALIGN=TOP><B>Description: </B> </TD><TD> <TEXTAREA NAME="description" ROWS=5 COLS=40></TEXTAREA></TD></TR>
<TR><TH COLSPAN=2><P><INPUT TYPE="submit" VALUE="Add Link"></P></TH></TR>
</TABLE>
</FORM>
<? mysql_close($cid); ?>
</BODY>
</HTML>
|
|

July 31st, 2008, 09:15 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 128
Thanks: 0
Thanked 1 Time in 1 Post
|
|
dabbler,
I am not sure about the mysql version you are using.
I assume it is version 4.1 or less because under
version 5 & up it uses mysqli (short for mysql improved) function instead of mysql. If you used v4.1 or less, did you save the file as
.php file extension. if v5.0 or up use mysqli objects and functions.
good luck.
john
|
|
The Following User Says Thank You to jmaronilla For This Useful Post:
|
|
|

July 31st, 2008, 02:12 PM
|
|
Registered User
|
|
Join Date: Oct 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi jmaronilla,
I'm using the latest version of MySQL (5.0 I think), as I've only recently downloaded it, along with the php and apache webserver.
I tried the mysqli that you suggested but got a few errors. I really can't work out what I'm doing wrong. it's a bit frustrating.
anyway, I started from scratch and set up a very basic connection script, just to see if MySQL server was communicating properly with the php engine. everything seems to be working fine with that.
I'm still puzzled at the above failure, I suspect it might be to do with the quote-marks (or lack thereof) around the strings in the functions.
anyway, this is the working connection and query, just to show that my set-up is ok:
<?php
mysql_connect("localhost", "useMe", "****") or die(mysql_error());
# echo "Connection to the server is good!<br/>";
mysql_select_db("my_db") or die(mysql_error());
# echo "Database selected!<br/>";
$result = mysql_query("SELECT * FROM projects");
# echo $result;
$row = mysql_fetch_assoc($result);
echo "Client ID: ".$row['clID']."<br />Name:".$row['sitename']
.", www:".$row['siteURL']."<br />Brief:".$row['description']."<br/>";
?>
|
|

July 31st, 2008, 07:47 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 128
Thanks: 0
Thanked 1 Time in 1 Post
|
|
dabbler,
you can't use mysql, try using mysqli instead or to be much simpler:
<?php
$dbo = new mysqli("localhost", "useMe", "****", "my_db") or die(mysqli_error());
$result = $dbo->query("SELECT * FROM projects");
# echo $result;
while($row = $result->fetch_object()){
echo '<p>';
echo 'Client ID: ' . $row->clID . '<br />';
echo 'Name: ' . $row->sitename . ', ';
echo 'www: ' . $row->siteURL . '<br />';
echo 'Brief: ' . $row->description;
echo '</p>';
}
?>
hopefully it should work.
let me know & good luck.
john
|
|

August 11th, 2008, 05:01 AM
|
|
Registered User
|
|
Join Date: Oct 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi John,
I think you might have missed the bottom of my previous response as the following works pretty well on my setup:
<?php
mysql_connect("localhost", "useMe", "****") or die(mysql_error());
# echo "Connection to the server is good!<br/>";
mysql_select_db("my_db") or die(mysql_error());
# echo "Database selected!<br/>";
$result = mysql_query("SELECT * FROM projects");
# echo $result;
$row = mysql_fetch_assoc($result);
echo "Client ID: ".$row['clID']."<br />Name:".$row['sitename']
.", www:".$row['siteURL']."<br />Brief:".$row['description']."<br/>";
?>
I don't really know why "mysql_" works, since as you say I should be using mysqli but it does and moreover, for some functions, mysqli generates errors (mysqli_query).
I'm certain I've setup PHP correctly and copied/pasted all the relevant libraries to win32/windows etc... and according to phpMyAdmin, everything is working correctly, so ... I'm a bit stumped why my original code and attempt still fails to work (as well as the modification using "mysqli").
Anyway, I thought I'd update you on how things went and also thank you for your assistance and time :)
|
|

August 12th, 2008, 03:08 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2007
Posts: 130
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
if your script is still not running then try
php tags as given below that is <?php intead of <?.
change every where. and then execute script.
<?
//this may not work if not configured
?>
<?php
//always works
?>
If above is not a problem then there is some syntax error,
If you can send me you script file then i would like to see it.
urt
Help yourself by helping someone.
|
|

August 13th, 2008, 09:27 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 128
Thanks: 0
Thanked 1 Time in 1 Post
|
|
dabbler,
i had this problem before. 1st to note when upgrading your version from <5.0 to 5 & higher is
you should have uninstall first your old version then install the new one.
merely installing the new version would conflict your configuration particularly the security.
An example is the PASSWORD function on the old version has 32 byte where as the new has 40 byte.uo
Also, check your php config if it supports the mysqli. if not, reconfigure it to support it.
hope this helps.
john
|
|
 |