devst3r
devst3r

Reputation: 562

SQL Query only fetching the first word from a string column

<?php
 include('common/connect.class.php');
include('common/admin.class.php');
session_start();
$user = $_SESSION['user'];

$con2 = new connection(); 
$con = $con2->connect();

$sql = "SELECT * FROM xam_category"; //Select Query
$myData = mysql_query($sql,$con) or die(mysql_error());;

echo "<table align='center'>
<tr>
<th>Category name</th>
<th>Category Description</th>
</tr>";
while($record = mysql_fetch_array($myData))
{
echo "<form action=user_book.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text size=10 readonly='true' name=usrname value=" .      $record['category_name'] . " </td>";
echo "<td>" . "<input type=text size=15 readonly='true' name=usrmail value=" . $record['category_desc'] . " </td>";
echo "<td>" . "<input type=submit name=delete value=DELETE" . " </td>";
echo "<td>" . "<input type=submit name=update value=UPDATE" . " </td>";
echo "</tr>";
echo "</form>";
} 
mysql_close($con);
echo "</table>";
?>

My SELECT query and data fetching is working fine. But, while i echo the fetched data, it only shows the first word "Computer" where it's actual value is "Computer Science".

The data stored in database viewed through PhpMyAdmin seems ok. But,

Data in the Database

The data shown in the .php page is different and not ok.

Data in the php page

I'm Stuck. How to display the right string from MySQL database on the PHP page?

Note: I'm using html inside echo to loop & display all data. Sorry, I have to use mysql() functions could'nt do msqli(). I also viewed other same type questions in StackOveflow. But, Couldn't find a solution.

Upvotes: 0

Views: 2220

Answers (2)

Jevgenijs Vaikulis
Jevgenijs Vaikulis

Reputation: 686

Try to use mysql_fetch_assoc instead of mysql_fetch_array if you would like to access to variable like $record['category_name'].

Please read this article: mysql_fetch_row() vs mysql_fetch_assoc() vs mysql_fetch_array()

Upvotes: 1

Jenz
Jenz

Reputation: 8369

If you have a value with a space in it, you must enclose that value within quotes in your HTML. Try with

echo "<td><input type=text size=10 readonly='true' name='usrname' value='".$record['category_name']."'</td>";

Upvotes: 3

Related Questions