我正在这个网站上使用php和mysql开发一个网站

时间:2016-11-01 05:18:55

标签: javascript php jquery mysql

我想从我的数据库中获取链接到网站的数据,我创建了一个搜索框,每当我在搜索框中输入特定值时,它应该显示数据库中的所有相关内容。我已经完成了下面的代码,它不是从数据库中获取数据,它只显示一个空白屏幕。

<?php
define('db_name','njgh');
define('db_user','root');
define('db_password','');
define('db_host','localhost');
session_start();

$link=mysql_connect(db_host,db_user,db_password);

if(!$link)
{
    die('couldnot connetc:'.mysql_error());
}

$db_selected=mysql_select_db(db_name,$link);
if(!$db_selected)
{
    die('cant connect to db');
}


function clean($str) {
        $str = @trim($str);
        if(get_magic_quotes_gpc()) {
            $str = stripslashes($str);
        }
        return mysql_real_escape_string($str);
}

$value = clean($_POST['searchtext']);
$qry = "SELECT * FROM database WHERE Site_ID = '$value'";
$result = mysql_query($qry);
print_r($result);

3 个答案:

答案 0 :(得分:1)

mysql_query函数返回资源ID而不是您的数据,使用 mysql_fetch_array mysql_fetch_object 来获取数据

$value = ($_POST['searchtext']);
$result = mysql_query("SELECT * FROM database WHERE Site_ID = '$value'");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    printf("ID: %s  Name: %s", $row[0], $row[1]);  
}

答案 1 :(得分:0)

自PHP 7.0起,

mysqli已完全删除。阻止SQL注入并使用<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "njgh"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $value = $_POST['searchtext']; /* create a prepared statement */ if($stmt = $conn->prepare("SELECT * FROM database WHERE Site_ID =?")) { /* bind parameters for markers */ $stmt->bind_param("s", $value); /* execute query */ $stmt->execute(); $result = $stmt->get_result(); if($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { echo $row['db_table_field_name']; } //or $row = $result->fetch_assoc(); print_r($row); } else { } /* close statement */ $stmt->close(); } /* close connection */ $con->close(); ?> 语句类。

{{1}}

答案 2 :(得分:0)

The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database. Instead, use the MySQLi or PDO_MySQL extensions. Hope this will work for you......

<?php
define('db_name','sports');
define('db_user','root');
define('db_password','');
define('db_host','localhost');
$link=mysqli_connect(db_host,db_user,db_password,db_name);
if(!$link)
{
    die('couldnot connect:'.mysql_error());
}

function clean($str) {
        $link=mysqli_connect(db_host,db_user,db_password,db_name);
        $str = @trim($str);
        if(get_magic_quotes_gpc())
            $str = stripslashes($str);

        return mysqli_real_escape_string($link, $str);
}
if(isset($_POST['searchtext'])){
$value = clean($_POST['searchtext']);
$qry = "SELECT * FROM cricket WHERE id = '$value'";
$qrydb = mysqli_query($link, $qry);

while($row=mysqli_fetch_array($qrydb)){
    $name = $row['name'];
}
echo $name ;
}
?>
<body>
    <form method="post">
      <input type="text" name="searchtext">
      <input type="submit" name="submit">
    </form>
</body>