Trying to make a simple script to create a simple learning sql

I am trying to learn php to access an sql database. I have tried a number of small scripts trying to make something work. So far I hit a wall.
MY current script is generating the following error message:

[php]Parse error: syntax error, unexpected T_STRING in /home/content/x/x/x/xxxxxxxx/html/phptest/newtest.php on line 7[/php]

Line 7 is the Create table command line -

The script is:
[php]<?php
// Connects to your Database
mysql_connect(“xxxxdata.db.9927794.hostedresource.com”, “username”, “xxxxxxxxxx”) or die(mysql_error());
mysql_select_db(“sengdata”) or die(mysql_error());
// create new table
//
CREATE TABLE friends (name VARCHAR(30), favcolor VARCHAR(30), favfood VARCHAR(30), pet VARCHAR(30));
//
// CREATE TABLE Persons (P_Id int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255))
//
INSERT INTO friends VALUES ( “Rose”, “Pink”, “Tacos”, “Cat” ), ( “Bradley”, “Blue”, “Potatoes”, “Frog” ),
( “Marie”, “Black”, “Popcorn”, “Dog” ), ( “Ann”, “Orange”, “Soup”, “Cat” )
//
// Collects data from “friends” table
$data = mysql_query(“SELECT * FROM friends”)
or die(mysql_error());
//
// puts the “friends” info into the $info array
$info = mysql_fetch_array( $data );
// Print out the contents of the entry
Print "Name: ".$info[‘name’] . " ";
Print "Pet: ".$info[‘pet’] . "
";
?> [/php]
I have had this same problem with several scripts and hit the same problems every time. I just want to get something working on MY database, then I can go from there.

this one just a learning script but in the end,
I am trying to create a simple php script that will allow me to enter an email name and then access my sql database and search for it. If it is there, say “the name is there” or if not there say this is a “new email name”.

Thanks for any and all Help
Randy

let me know if this works for you:

[php]

<?php // Connects to your Database mysql_connect("xxxxdata.db.9927794.hostedresource.com", "username", "xxxxxxxxxx") or die(mysql_error()); mysql_select_db("sengdata") or die(mysql_error()); // create new table // mysql_query("CREATE TABLE friends (name VARCHAR(30), favcolor VARCHAR(30), favfood VARCHAR(30), pet VARCHAR(30))"); // CREATE TABLE Persons (P_Id int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255)) // mysql_query("INSERT INTO friends (`name`, `favcolor`, `favfood`, `pet`) VALUES ('Rose', 'Pink', 'Tacos', 'Cat'), ('Bradley', 'Blue', 'Potatoes', 'Frog'), ('Marie', 'Black', 'Popcorn', 'Dog'), ('Ann', 'Orange', 'Soup', 'Cat')"); // // Collects data from "friends" table $data = mysql_query("SELECT * FROM friends") or die(mysql_error()); // // puts the "friends" info into the $info array $info = mysql_fetch_array( $data ); // Print out the contents of the entry Print "Name: ".$info['name'] . " "; Print "Pet: ".$info['pet'] . "
"; ?>

[/php]

you might want to have the query check before it create the table or it will reult in a error
for example

[php]
mysql_query(“CREATE TABLE IF NOT EXISTS friends (name VARCHAR(30), favcolor VARCHAR(30), favfood VARCHAR(30), pet VARCHAR(30))”);

[/php]

only a sugestion

Yes, that works!

I have not found where it has told me to place the create table commands inside the mysql_query command. I have tried at least 4 or 5 other script samples and NONE of them had that. (none of them worked either)

Wow, finally, what a great feeling!
Now, I can have a working base line to actually play with to learn more.

I am going to go play with my pets and flavors until I fully understand what I created…

Thank You!
Randy

So I run the script plintu modified for me a couple of times with no errors on subsequent runs. Then I added the “if not exists” statement, as you show, but the script ran the same as before.
Why did I not get an error without the “if not exist”?

Without the “if not exist” am I re-writing the table and data each time, I run the script?

Thanks
Randy

some mysql errors are silent and will not send the error back to you unless you request it (it has to do with server settings). for example run this script and see if you get the error

[php]

<?php // Connects to your Database mysql_connect("xxxxdata.db.9927794.hostedresource.com", "username", "xxxxxxxxxx") or die(mysql_error()); mysql_select_db("sengdata") or die(mysql_error()); // create new table // mysql_query("CREATE TABLE friends (name VARCHAR(30), favcolor VARCHAR(30), favfood VARCHAR(30), pet VARCHAR(30))") or die(mysql_error()); // CREATE TABLE Persons (P_Id int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255)) // mysql_query("INSERT INTO friends (`name`, `favcolor`, `favfood`, `pet`) VALUES ('Rose', 'Pink', 'Tacos', 'Cat'), ('Bradley', 'Blue', 'Potatoes', 'Frog'), ('Marie', 'Black', 'Popcorn', 'Dog'), ('Ann', 'Orange', 'Soup', 'Cat')"); // // Collects data from "friends" table $data = mysql_query("SELECT * FROM friends") or die(mysql_error()); // // puts the "friends" info into the $info array $info = mysql_fetch_array( $data ); // Print out the contents of the entry Print "Name: ".$info['name'] . " "; Print "Pet: ".$info['pet'] . "
"; ?> [/php]

notice that I added
[php]or die(mysql_error())[/php]

this should force the error to your screen for you to view

That was cool! :o

I understand and I appreciate your help!!

Sponsor our Newsletter | Privacy Policy | Terms of Service