0

i'm a newbie to php still. I'm using phpmyadmin as my database. I have a table called 'lessonno' and a column named 'lesson' in it. I tried using this code to retrieve out the number inside 'lesson'. But it's not printing out anything. Can someone help?

<?php 
$server = 'localhost'; 
$username = ''; 
$password = ''; 
$database = 'project'; 
mysql_connect($server,$username,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());


$sql = "SELECT 'lesson' FROM 'lessonno'";
$lesson = $_POST['lesson']; 
 $result = mysql_query($sql);
?>
<?php
    for($i = 1; $i <= $lesson; $i++) {    
echo "<div>
    <a href=\"k1levelX.php?lesson=".$i."\"><span>Lesson ".$i."</span></a>           
</div>
<br>";
    }

?>
6
  • when you execute the select query inside phpmy admin,didd u get any value? Commented Aug 14, 2014 at 5:43
  • did u check whether $lesson is getting the value or not? Commented Aug 14, 2014 at 5:44
  • try to die(mysql_error()) like $result = mysql_query($sql) or die(mysql_error()); and see what is the error comes Commented Aug 14, 2014 at 5:44
  • 1
    you are printing $i, which is loop index!! There isn't any use of $result here, then how will you get it?? Commented Aug 14, 2014 at 5:45
  • you are not using any variable to fetch the result. Commented Aug 14, 2014 at 5:50

4 Answers 4

1

You can use something like this:

$sql = "SELECT lesson FROM lessonno";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo $row['lesson'];
}

If you would like to only print out a specific lesson with an certan ID, you can use something along the lines:

$id = $_GET['lessonid']; // If you would have something like index.php?lessonid=36 and you'd like it to only fetch the data for the lesson with the id of 36. 
$sql = "SELECT lesson FROM lessonno WHERE id='$id'";

(by looking at the $_POST['lesson'] part, I suppose that's something you might be trying to do as it's in the for loop as well)

Also, I suggest you use mysqli.

And, this:

echo "<div>
    <a href=\"k1levelX.php?lesson=".$i."\"><span>Lesson ".$i."</span></a>           
</div>
<br>";

Will just echo the $i as both lesson= and the span with Lesson, which won't grab any information from the actual database but just go with the current number it's at, from the for loop you have.

Sign up to request clarification or add additional context in comments.

2 Comments

Also, I suggest you use mysqli.: Using mysqli with that unsafe string interpolated query will be as bad as using mysql
@Hanky웃Panky I never said that he should use it with this specific query. I'm just saying that he should get into using mysqli instead of mysql, and if he does that he might also get into how he should do the queries. :)
1

i have made some changes in your code try this

<?php 
$server = 'localhost'; 
$username = 'root'; 
$password = ''; 
$database = 'project'; 
$conn = mysql_connect($server,$username,$password) or die(mysql_error());
mysql_select_db($database, $conn) or die(mysql_error());


$sql = "SELECT `lesson` FROM `lessonno`";
$lesson = $_POST['lesson']; 
$result = mysql_query($sql) or die(mysql_error());


while($row = mysql_fetch_assoc($result))
{
   $lesson_no = $row['lesson'];
echo "<div>
    <a href=\"k1levelX.php?lesson=".$lesson_no."\"><span>Lesson ".$lesson_no."</span></a>           
</div>
<br>";
    }

?>

Note : mysql_* is deprecated. use mysqli_* OR PDO

4 Comments

@SKRocks I'm not sure why but it keep displaying this error message "Notice: Undefined index: lesson in C:\xampp\htdocs\project\k1levelselect.php on line 23"
@Chunkeat have you lesson column in your table lessonno
@SKRocks Yes i have the lesson column in my table lessonno
@Chunkeat check there is space in in column lesson before name or after? you can check this by execute query in your phpmyadmin directly
0

For getting Values from DB you need to use something like this

while ($row = mysql_fetch_assoc($result)) {
 print_r($row);   
}

For further reference please visit https://www.php.net/manual/en/function.mysql-fetch-assoc.php

Comments

0

For counting the number of data in your database, just insert this code

$sql = "SELECT 'lesson' FROM 'lessonno'";
$lesson = $_POST['lesson']; 
$result = mysql_query($sql);
$count=mysql_num_rows($result);//this will count the number of rows in your table.

echo "<div>
<a href=\"k1levelX.php?lesson=".$count."\"><span>Lesson ".$count."</span></a>           
</div>
<br>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.