Hey, I've been trying to code my own news/ blogging script, by following a tutorial, but unfortunately I keep getting these mysql errors when I submit an entry, and on the page where I want it to be displayed. And I don't know how to recognize the errors just by looking at the number line, or how to fix it.

index.php

<?php 

//$hostname="placid1.placid-soul.org";
$user="xxx"; //user name to access database
$pass= "xxx"; //password 
$dbase="xxx"; //database name

$q = "select * from blog order by date desc "; 
$result= mysql_query($q, $connection) or die 
("Could not execute query : $q." . mysql_error()); 

// dynamic navigation variables 
$rows_per_page=1; // adjust the number here to display number of entries per page 
$total_records=mysql_num_rows($result); 
$pages = ceil($total_records / $rows_per_page); 

$screen = $_GET["screen"]; 
if (!isset($screen)) 
$screen=0; 
$start = $screen * $rows_per_page; 
$q .= "LIMIT $start, $rows_per_page"; 
$result= mysql_query($q, $connection) or die 
("Could not execute query : $q." . mysql_error()); 

while ($row=mysql_fetch_array($result)) 
{ 
    $id=$row["id"]; 
    $name=$row["name"]; 
    $email=$row["email"]; 
    $entry=$row["entry"]; 
    $date=$row["date"]; 
    $icon=$row["icon"]; 
    $title=$row["title"]; 

?> 

    <table width="345" border="0" cellspacing="1" cellpadding="0" class="updates"> 
    <tr> 
    <td><h1><?php echo "$title"; ?></h1></td> 
    </tr> 
    <tr> 
    <td> 
    <img src="<?php echo "$icon"; ?>" alt="icon" align="left"><?php echo "$entry"; ?>
    <p>Posted by <a href="mailto:<?php echo "$email"; ?>"><?php echo "$name"; ?> on <?php echo "$date"; ?>.</p> 
    </td> 
    </tr> 
    </table> 
     
    <div align="center"> 

<?php 
} #end of while 

// Display dynamic navigation here 

// create the dynamic links 
if ($screen > 0) { 
    $j = $screen - 1; 
    $url = "display.php?screen=$j"; 
    echo "<a href=\"$url\">Prev</a>"; 
} 

// page numbering links now 

for ($i = 0; $i < $pages; $i++) { 
    $url = "display.php?screen=" . $i; 
    $j = $i + 1; 
    echo " | <a href=\"$url\">$j</a> | "; 
} 

if ($screen < $pages-1) { 
    $j = $screen + 1; 
    $url = "display.php?screen=$j"; 
    echo "<a href=\"$url\">Next</a>"; 
} 

?> 

</div>

<?php include 'bottom.php'; ?>

submit.php

<?php //
$hostname="xxx";
$user="xxx"; //user name to access database
$pass= "xxx"; //password 
$dbase="xxx"; //database name
// For Global Registers off 

$name = strip_tags($_POST["name"]); 
$email = strip_tags($_POST["email"]); 
$entry = strip_tags($_POST["entry"]); 
$title = strip_tags($_POST["title"]); 
$icon =  strip_tags($_POST["icon"]); 

// Check if user submit blank entry 
if ($name == "" || $email == "" || $entry == "" || $title == "") 
{  
    die ("You must fill in all fields, please click back and try again."); 
} 

else { 
    $q="insert into blog (id,name,title,email,entry,date,icon) VALUES ('','$name','$title','$email','$entry',now(),'$icon')"; 
    $result = mysql_query($q); 
     
    if ($result)  
    { 
        echo "thank you, blog has been submitted."; 
    } 

}  
?>

And these are the errors:
index.php

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/.fiorello/felaigne/placid-soul.org/index.php on line 45
Could not execute query : select * from blog order by date desc .

submit.php

Warning: mysql_query(): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /home/.fiorello/felaigne/placid-soul.org/submit.php on line 22

Warning: mysql_query(): A link to the server could not be established in /home/.fiorello/felaigne/placid-soul.org/submit.php on line 22

I've tried looking through the search feature, and I couldn't find another thread with the same problem I have, so I thought I should create a new topic. I've tried my best to explain it, so that it's understandable at least. And if you need anymore information just let me know. =) Thank you, in advance.

Dani AI

Generated

Brief diagnosis and practical fixes (based on ’s posts and ’s comment):

Both error sets were environment/logic problems rather than mysterious PHP bugs. The “supplied argument is not a valid MySQL-Link resource” / socket error means no successful DB connection was established (either mysql_connect was never called, the host was wrong, or the returned link was false). The “Invalid ID specified.” message came from the display script intentionally dying when no numeric id was present — a common mismatch when one page uses ?screen= while the other expects ?id=.

Concrete steps to fix and harden the code

  • Make sure the connection is created and checked before any mysql/mysqli queries, and keep the link in a variable used by subsequent calls. For modern code, use mysqli or PDO and prepared statements. Example (mysqli):
$mysqli = new mysqli($host, $user, $pass, $db);
if ($mysqli->connect_errno) { die('DB connect error: '.$mysqli->connect_error); }
$stmt = $mysqli->prepare('SELECT title, entry, date FROM blog WHERE id = ? LIMIT 1');
$stmt->bind_param('i', $id);
  • If sticking with legacy code, ensure $connection = mysql_connect(...); mysql_select_db(..., $connection); runs before any mysql_query() and that the same $connection (or the last opened link) is used.

  • Guard the id check so it fails gracefully or falls back to a list view instead of dying. Example pattern:

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id <= 0) { /* show recent posts or paging instead of die() */ }

Other useful tips

  • When MySQL returns DATETIME strings, convert with strtotime() before passing to date() (or use UNIX_TIMESTAMP() in the query).
  • If a socket error appears on localhost, try 127.0.0.1 to force TCP, or confirm the DB server is running and the host/port are correct.
  • strip_tags() removes HTML but does not prevent SQL injection — prefer prepared statements or proper escaping.
  • ’s extra parentheses are fine for clarity, but they wouldn’t fix a missing connection or a mismatched GET parameter.

Quick checklist: confirm the correct script URL (with ?id= if needed), establish and check the DB connection first, and use parameterized queries plus safe output (e.g., htmlspecialchars) when echoing content.

Recommended Answers

All 4 Replies

I can't seem to figure out how to edit my previous post. =\ So I'm sorry for double posting but anyways, I've solved my submission error problem, and I re-did the whole thing by using another tutorial. I had more success with it then the last one. (if you'd like the link let me know.) The only problem I'm getting now is that, my posts won't display, and it shows this instead:

Invalid ID specified.

I can't seem to figure out what's causing it to be invalid. I asked my friends about it, and they seem to think it's this line of code:

if(!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    die("Invalid ID specified.");
}

There seems to something wrong with the top line, my friends and I were assuming that. Any suggestions? Also here's the new display code on index.php

<?php
mysql_connect ('xxx', 'xxx', 'xxx') ;
mysql_select_db ('xxx');

if(!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    die("Invalid ID specified.");
}

$id = (int)$_GET['id'];
$sql = "SELECT * FROM php_blog WHERE id='$id' LIMIT 1";

$result = mysql_query($sql) or print ("Can't select entry from table php_blog.<br />" . $sql . "<br />" . mysql_error());

while($row = mysql_fetch_array($result)) {

    $date = date("l F d Y", $row['timestamp']);

    $title = $row['title'];
    $entry = $row['entry'];

    ?>

    <p><strong><?php echo $title; ?></strong><br /><br />
    <?php echo $entry; ?><br /><br />
    Posted on <?php echo $date; ?>
    </p>

    <?php
}

?>

<?php include 'bottom.php'; ?>

The problem is in the sintax of the if statement. When you write a if clause with two argument you shoulw write it like

if ( (firt argument) || (second argument) )

I think that's the problem with your code.
You should change your code to

if ((!isset($_GET['id'])) || (!is_numeric($_GET['id']))) {
die("Invalid ID specified.");
}

Thanks for the input, but unfortunately, I've tried changing my code to that, and the entrie(s) still won't display. I still get this:

Invalid ID specified.

Nevermind, I found out my error, I kept going to the wrong location. xD;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.