Page 1 of 1

Need help on functions. Im a newbie using WAMP server.

Posted: Fri May 09, 2008 6:26 am
by simarjot
I shall be highly obliged if someone could solve my following problem. Im a newbie in php and i am learning php through W3C, VTC, Lynda.com and PHPVideoTutorials.com. I am practicing my php on the open source WAMP Server2. I don't know how much WAMP is worth or that i am a bad programmer as i unable to execute this simple php code. Though the basic echo command and loops that i tested on till now are perfectly working but the following functions are not working.

The first code was written by me and the second code i copied from the VTC example file when i lost faith in my coding. Both are not working. I have even reinstalled the WAMP Server but situation is still not good. I even looked out on W3C and PHP manual and my code is as similar as mentioned on these two resources.

Is this possible that the functions of WAMP server2 may somehow could have got corrupted or even that the WAMP is not properly connecting PHP and MySQL???

Please help me out..........
======================================================================

Code: Select all

//This code was written by me for connecting to the database:
 
$con = mysql_connect("localhost");
$sel = mysql_select_db("vinyldealers", $con);
$query = "select * from shops";
$result = mysql_query($query, $con);
$table = mysql_field_table($result, 0);
echo $table;
while ($record = mysql_fetch_row($result))
    {
     for ($i=0; $i<count($record); $i++)
        {
            echo $record[$i]."<br />";
        }
     echo "<hr />";
    }
 
//The following error was reported:
 
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\wamp\www\php_prac_files\hello world.php on line 48

======================================================================

Code: Select all

//This is the code from the VTC example file:
 
<?php
// make a connection to mysql, using the local IP
$db = mysql_connect("localhost");
 
// select the 'vinyldealers' database
mysql_select_db("vinyldealers", $db);
 
// declare a string variable and set it to a simple
// string containing the SQL query we want to output
// nb: *don't* put a semicolon after the SQL string!! <!-- s:) --><img src=\"{SMILIES_PATH}/icon_smile.gif\" alt=\":)\" title=\"Smile\" /><!-- s:) -->
$query = "SELECT * FROM shops";
 
// run the query specified in the last line
$result = mysql_query($query);
 
// loop through each row in the result of that query
// and return the row as an associative array
while ($record = mysql_fetch_assoc($result)) {
 
    // loop through each element of the array and assign
    // the names of the element's key to variable 'fieldname'
    // and the value of the element to variable 'fieldvalue'
    while (list ($fieldname, $fieldvalue) = each ($record)) {
 
        // output each key and value
        echo $fieldname.": ".$fieldvalue."<BR>";
    }
 
    // an extra linebreak to split up the rows
    echo "<BR>";
}
?>
//The following error was reported:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\php_prac_files\showdata.php on line 19
==========================================================================

For reference purpose i am also copying here the code of the MySQL database schema from the VTC example file:

[sql]# MySQL-Front Dump 2.0## Host: localhost   Database: vinyldealers#--------------------------------------------------------# Server version 3.23.33  # This database is a sample MySQL database for use with# the VTC training guide 'PHP', author: Joshua Mostafa.## This SQL exported using MySQL-Front, a freeware Windows GUI# for MySQL, created by Angar Becker (info@anse.de).#        http://www.mysqlfront.de/# # MySQL information and downloads available here:#        http://www.mysql.com/ DROP DATABASE IF EXISTS vinyldealers; CREATE DATABASE vinyldealers; USE vinyldealers;  ## Table structure for table 'feedback'# CREATE TABLE `feedback` (`feedbackID` tinyint(3) UNSIGNED NOT NULL AUTO_INCREMENT,`user` varchar(50) NOT NULL DEFAULT '',`email` varchar(50) NOT NULL DEFAULT '',`spam` tinyint(4) NOT NULL DEFAULT '0',`comments` text NOT NULL,PRIMARY KEY (`feedbackID`),UNIQUE KEY `feedbackID`(`feedbackID`)) TYPE=MyISAM;   ## Dumping data for table 'feedback'# # (empty table)  ## Table structure for table 'genres'# CREATE TABLE `genres` (`genreID` tinyint(3) UNSIGNED NOT NULL AUTO_INCREMENT,`name` char(20) NOT NULL DEFAULT '0',PRIMARY KEY (`genreID`),UNIQUE KEY `genreID`(`genreID`)) TYPE=MyISAM;   ## Dumping data for table 'genres'# INSERT INTO `genres` VALUES("1","Techno");INSERT INTO `genres` VALUES("2","Trance");INSERT INTO `genres` VALUES("3","Drum & Bass");INSERT INTO `genres` VALUES("4","Hiphop");INSERT INTO `genres` VALUES("5","Breaks");INSERT INTO `genres` VALUES("6","House");INSERT INTO `genres` VALUES("7","Hardcore");INSERT INTO `genres` VALUES("8","US Garage");INSERT INTO `genres` VALUES("9","UK Garage");INSERT INTO `genres` VALUES("10","Funk");INSERT INTO `genres` VALUES("11","Electro");INSERT INTO `genres` VALUES("12","Gabba");INSERT INTO `genres` VALUES("13","Reggae");INSERT INTO `genres` VALUES("14","Dancehall");INSERT INTO `genres` VALUES("15","Disco");INSERT INTO `genres` VALUES("16","Dub");  ## Table structure for table 'shopgenres'# CREATE TABLE `shopgenres` (`shopID` tinyint(3) UNSIGNED NOT NULL DEFAULT '0',`genreID` tinyint(3) UNSIGNED NOT NULL DEFAULT '0') TYPE=MyISAM;   ## Dumping data for table 'shopgenres'# INSERT INTO `shopgenres` VALUES("1","1");INSERT INTO `shopgenres` VALUES("1","2");INSERT INTO `shopgenres` VALUES("1","3");INSERT INTO `shopgenres` VALUES("1","5");INSERT INTO `shopgenres` VALUES("1","7");INSERT INTO `shopgenres` VALUES("1","11");INSERT INTO `shopgenres` VALUES("2","1");INSERT INTO `shopgenres` VALUES("2","3");INSERT INTO `shopgenres` VALUES("2","5");INSERT INTO `shopgenres` VALUES("2","6");INSERT INTO `shopgenres` VALUES("2","7");INSERT INTO `shopgenres` VALUES("2","8");INSERT INTO `shopgenres` VALUES("2","9");INSERT INTO `shopgenres` VALUES("2","11");INSERT INTO `shopgenres` VALUES("3","3");INSERT INTO `shopgenres` VALUES("3","4");INSERT INTO `shopgenres` VALUES("3","7");INSERT INTO `shopgenres` VALUES("3","9");INSERT INTO `shopgenres` VALUES("3","13");INSERT INTO `shopgenres` VALUES("3","14");INSERT INTO `shopgenres` VALUES("4","1");INSERT INTO `shopgenres` VALUES("4","2");INSERT INTO `shopgenres` VALUES("4","5");INSERT INTO `shopgenres` VALUES("4","6");INSERT INTO `shopgenres` VALUES("4","3");INSERT INTO `shopgenres` VALUES("4","8");INSERT INTO `shopgenres` VALUES("5","3");INSERT INTO `shopgenres` VALUES("5","4");INSERT INTO `shopgenres` VALUES("5","7");INSERT INTO `shopgenres` VALUES("5","9");INSERT INTO `shopgenres` VALUES("5","10");INSERT INTO `shopgenres` VALUES("5","14");INSERT INTO `shopgenres` VALUES("6","4");INSERT INTO `shopgenres` VALUES("6","10");INSERT INTO `shopgenres` VALUES("2","15");INSERT INTO `shopgenres` VALUES("2","10");INSERT INTO `shopgenres` VALUES("7","1");INSERT INTO `shopgenres` VALUES("7","2");INSERT INTO `shopgenres` VALUES("7","6");INSERT INTO `shopgenres` VALUES("8","1");INSERT INTO `shopgenres` VALUES("8","3");INSERT INTO `shopgenres` VALUES("8","7");INSERT INTO `shopgenres` VALUES("8","2");INSERT INTO `shopgenres` VALUES("8","12");INSERT INTO `shopgenres` VALUES("9","6");INSERT INTO `shopgenres` VALUES("9","15");INSERT INTO `shopgenres` VALUES("10","10");INSERT INTO `shopgenres` VALUES("10","5");INSERT INTO `shopgenres` VALUES("10","11");INSERT INTO `shopgenres` VALUES("10","3");INSERT INTO `shopgenres` VALUES("10","9");INSERT INTO `shopgenres` VALUES("3","16");INSERT INTO `shopgenres` VALUES("5","16");  ## Table structure for table 'shops'# CREATE TABLE `shops` (`shopID` tinyint(3) UNSIGNED NOT NULL AUTO_INCREMENT,`name` varchar(40) NOT NULL DEFAULT '',`address1` varchar(40) DEFAULT NULL,`address2` varchar(40) DEFAULT NULL,`suburb` varchar(20) DEFAULT NULL,`phone` varchar(8) NOT NULL DEFAULT '',`fax` varchar(8) NOT NULL DEFAULT '',`email` varchar(40) NOT NULL DEFAULT '',`website` varchar(40) NOT NULL DEFAULT '',`comments` varchar(50) NOT NULL DEFAULT '',PRIMARY KEY (`shopID`),UNIQUE KEY `shopID`(`shopID`)) TYPE=MyISAM;   ## Dumping data for table 'shops'# INSERT INTO `shops` VALUES("1","Bleep Bleep Records","14 Industrial Ave","","Troughton","65465465","64351231","bleepbleep@freeemail.com","","Interesting UV-glow clubwear also on sale");INSERT INTO `shops` VALUES("2","Hi Fly Sounds","15 Outersdale Road","","Imblendon","64231615","","hiflier@hotmail.com","","");INSERT INTO `shops` VALUES("3","Tuff Tune","13 Yard St","","Troughton","64321654","64351684","tufftune@yahoo.com","","Specialist t\'ing ..");INSERT INTO `shops` VALUES("4","Deeper Beat Records","53 Blempton Ave","","Durringvale","64231654","64615644","info@deeperbeat.com.au","www.deeperbeat.com.au","");INSERT INTO `shops` VALUES("5","Riddim Records","37 Dorset Road","","Hellington","64234516","","manager@riddim.com.au","www.riddim.com.au","Rudeboy riddims");INSERT INTO `shops` VALUES("6","Phat Bass Music","91 Bevelled Road","Long Park","Sirdale","64321684","64351568","phatbass@mymail.com","","Loud flavas");INSERT INTO `shops` VALUES("7","Minimalism","24 Crane St","","Hellington","64321684","64321684","us@minimalism.com.au","www.minimalism.com.au","Seriously stripped-down techno");INSERT INTO `shops` VALUES("8","Explosive Records","71 Bang St","","Troughton","64321684","64651684","bang@hotmail.com","","");INSERT INTO `shops` VALUES("9","Fab Sounds","21 Enders Lane","","Imbledon","64231654","64654651","tony@fabsound.com.au","www.fabsound.com.au","Gorgeous toons daarling!");INSERT INTO `shops` VALUES("10","Freaky Beat","85 Ungoliant Ave","Wyrd Hills","Trippiton","64321654","","str@oddside.com.au","","Very, very odd.");[/sql]

Re: Need help on functions. Im a newbie using WAMP server.

Posted: Fri May 09, 2008 9:13 am
by lornajane
I think you are quite right that you may not be connected to mysql. Try this:

$con = mysql_connect("localhost");
var_dump($con);

You should see output saying something about "Resource". If not, then you aren't connecting to mysql. Check you can connect to mysql from the command line or another tool, then check your settings. I think you might need to give mysql_connect a username (and maybe password). The syntax for that would be:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');

Re: Need help on functions. Im a newbie using WAMP server.

Posted: Fri May 09, 2008 9:15 am
by John Cartwright
A better debugging method is using mysql_error()

Code: Select all

$con = mysql_connect("localhost") or die(mysql_error());
Also, please use code tags :evil:

Re: Need help on functions. Im a newbie using WAMP server.

Posted: Sat May 10, 2008 2:11 am
by RobertGonzalez
You are not connected to the database server. As someone else suggested, try using mysql_error() to get information the last error.