Current location: Hot Scripts Forums » Programming Languages » PHP » Trying to read and write from a table


Trying to read and write from a table

Reply
  #1 (permalink)  
Old 10-21-08, 12:47 PM
tirengarfio tirengarfio is offline
Newbie Coder
 
Join Date: Oct 2008
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Trying to read and write from a table

Hi,

im trying to write a code which writes and reads a string in a field of a table, but i get this error below at the line that starts with "$row = mysql_fet..."

Code:
 "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL 
result resource in /opt/lampp/htdocs/Sites/prueba.php on line 23"

This is the code:

PHP Code:

<?php


$nombre
="Javier";

$nombre2;


if (!(
$link=mysql_connect("localhost","","")))
{
echo 
"Error conectando a la base de datos.";
exit();
}

mysql_db_query("ejemplo""insert into clientes(nombre) values('$nombre')");

$result mysql_db_query("ejemplo""select * from clientes");

$row mysql_fetch_array($result);

echo 
$row["nombre"];


?>

Anyone can help me?

Ciao
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 10-21-08, 12:59 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
mysql_db_query() was deprecated a long... very long time ago.

Use mysql_query() and mysql_select_db() instead.

PHP Code:

<?php

$nombre
="Javier";

$nombre2;


if (!
$link mysql_connect("localhost","",""))
{
    echo 
"Error conectando a la base de datos.";
    exit();
}

if (!
mysql_select_db("ejemplo"))
{
    echo 
"Error conectando a la base de datos.";
    exit();
}

mysql_query("insert into clientes(nombre) values('$nombre')") OR die(mysql_error());

$result mysql_query("select * from clientes") OR die(mysql_error());
$row mysql_fetch_array($result);

echo 
$row["nombre"];

?>
www.php.net/mysql_select_db
www.php.net/mysql_query
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 10-22-08, 04:59 PM
tirengarfio tirengarfio is offline
Newbie Coder
 
Join Date: Oct 2008
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Nico,

your code gives this error:

Code:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /opt/lampp/htdocs/Sites/prueba.php on line 23
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 10-22-08, 05:01 PM
ianbrind ianbrind is offline
Wannabe Coder
 
Join Date: Jul 2008
Location: Somerset UK!
Posts: 192
Thanks: 0
Thanked 0 Times in 0 Posts
That is because there is no connection to the database.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 10-22-08, 05:32 PM
xEX3CUT1ONx xEX3CUT1ONx is offline
Wannabe Coder
 
Join Date: Jun 2008
Posts: 116
Thanks: 2
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by ianbrind View Post
That is because there is no connection to the database.
I believe there is he has just taken out the username and password so we cannot see
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 10-23-08, 10:23 AM
tirengarfio tirengarfio is offline
Newbie Coder
 
Join Date: Oct 2008
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by ianbrind View Post
That is because there is no connection to the database.

!?!...but, if i can't see the error message about the connection, why do you say that??
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 10-23-08, 11:15 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Nico's code works fine if you have
a database named "ejemplo"
with a table named "clientes"
that has a column named "nombre".
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 10-25-08, 04:26 PM
tirengarfio tirengarfio is offline
Newbie Coder
 
Join Date: Oct 2008
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by job0107 View Post
Nico's code works fine if you have
a database named "ejemplo"
with a table named "clientes"
that has a column named "nombre".
Hi,

thanks, i think your are wrong, don't you? :

Code:
mysql> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema | 
| cdcol              | 
| ejemplo            | 
| mysql              | 
| phpmyadmin         | 
| prueba             | 
| test               | 
+--------------------+
7 rows in set (0.11 sec)

mysql> use ejemplo
Database changed
mysql> show tables;
+-------------------+
| Tables_in_ejemplo |
+-------------------+
| clientes          | 
+-------------------+
1 row in set (0.00 sec)

mysql> Describe clientes;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| nombre   | varchar(100) | YES  |     | NULL    |       | 
| telefono | varchar(100) | YES  |     | NULL    |       | 
+----------+--------------+------+-----+---------+-------+
2 rows in set (0.07 sec)

mysql>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 10-26-08, 08:36 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by tirengarfio View Post
Hi,

thanks, i think your are wrong, don't you? :

Code:
mysql> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema | 
| cdcol              | 
| ejemplo            | 
| mysql              | 
| phpmyadmin         | 
| prueba             | 
| test               | 
+--------------------+
7 rows in set (0.11 sec)

mysql> use ejemplo
Database changed
mysql> show tables;
+-------------------+
| Tables_in_ejemplo |
+-------------------+
| clientes          | 
+-------------------+
1 row in set (0.00 sec)

mysql> Describe clientes;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| nombre   | varchar(100) | YES  |     | NULL    |       | 
| telefono | varchar(100) | YES  |     | NULL    |       | 
+----------+--------------+------+-----+---------+-------+
2 rows in set (0.07 sec)

mysql>
No, I am not wrong and neither is Nico.
You are doing something wrong some where.
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 10-27-08, 04:46 AM
tirengarfio tirengarfio is offline
Newbie Coder
 
Join Date: Oct 2008
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by job0107 View Post
You are doing something wrong some where.
Ok job0107, but... with the information i have posted (tables and code), is not enough to know Where is the error?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you read and write to files dacksal PHP 5 05-15-08 11:12 PM
simple text read and write script Kevin Raleigh Script Requests 1 08-19-06 05:16 PM
Heres one, mysql statement to load a table with a file from ftp?? 0o0o0 PHP 8 04-28-06 02:32 AM
Can javascript be used to read and write to a text file kept on a webserver? gmb1994 JavaScript 4 11-23-04 11:50 AM
Database file or object file is read only, w_inaim ASP 2 09-21-04 03:18 PM


All times are GMT -5. The time now is 02:53 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.