简体   繁体   English

Mysql查询,比较值并分配给PHP变量

[英]Mysql Query, comparing values and assigning to PHP variables

I have done a fair bit of research into what i want to do, although i haven't found anything. 尽管我什么都没找到,但我对我想做的事情做了相当多的研究。 I am not too sure if i am looking for the right thing :( I am also a little bit new to PHP and MySQL syntax, so please be kind. 我不太确定我是否正在寻找正确的东西:(我对PHP和MySQL语法还是有点陌生​​,所以请客气。

I wish to perform the following in this order: 我希望按以下顺序执行以下操作:

  1. Connect to a database (DONE) 连接到数据库(完成)
  2. Query for a specific string (I think im done) From here is gets a bit fuzzy :( 查询特定的字符串(我想我完成了)从这里开始有点模糊:(
  3. If a match is found for the variable, copy the whole row (I need other variables). 如果找到匹配的变量,则复制​​整行(我需要其他变量)。
  4. Assign the values from the SQL query to a PHP variables. 将SQL查询中的值分配给PHP变量。
  5. From there i will be right to carry on. 从那里我将继续下去。

I have established the connection to the database with the following: 我已经通过以下方式建立了到数据库的连接:

function connect() {
$dbname = 'database';
$dbuser = 'username';
$dbpass = 'password';
$dbhost = 'localhost';
mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to connect to database");
}

And then calling the function connect(); 然后调用函数connect();

I then wish to query the database for a particular value, for the sake of this argument i will use a static value. 然后,我希望在数据库中查询一个特定的值,由于这个参数,我将使用一个静态值。 This is what i have: 这就是我所拥有的:

mysql_select_db(DATABASENAME) or die( "Unable to select database");
$query = "SELECT * FROM `TABLE` WHERE `COLUMN` LIKE 'VAULE'";
$result=mysql_query($query);

From here i am not too sure how to compare the query result to see if it is a match (something along the lines of mysql rows?). 从这里我不太确定如何比较查询结果以查看是否匹配(类似于mysql行?)。 If there is a match, then i would like to obtain the entire row, and assign each value to a php variable. 如果有匹配项,那么我想获取整行,并将每个值分配给一个php变量。

I am not asking for you to do it for me, simply i kick in the right direction should be fine! 我不是要你为我做,只是我朝正确的方向踢应该没问题!

Hope it explains it enough :) 希望它能解释得足够:)

Thanks for your kind guidance 多谢您的指导

Where the hell you learned how to use MySQL in PHP ? 您到底在哪里学习了如何在PHP中使用MySQL? The mysql_* functions are more then 10 years old and not maintained anymore. mysql_*函数已使用10年以上,并且不再维护。 Community has already begun to work on deprecating them . 社区已经开始致力于弃用它们

You should be using PDO or MySQLi for that. 您应该为此使用PDOMySQLi

// connection to database
$db = new PDO('mysql:host=localhost;dbname=datadump_pwmgr;charset=UTF-8', 
               'datadump_pwmgr', 
               'kzddim05xrgl');
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

// setting up prepared statement for the query
$statement = $db->prepare('SELECT * FROM table WHERE column LIKE :value');
$statement->bindParam(':value', $some_variable, PDO::PARAM_STR, 127);

// executing query and fetching first result
if ( $statement->execute())
{
    $data = $statement->fetch(PDO::FETCH_OBJ);
    var_dump( $data );
}

This should give you something like what you needed. 这应该为您提供所需的东西。 Though, I would recommend to try this tutorial . 不过,我建议您尝试本教程 And learning more about prepared statements could be useful too. 学习更多有关预备语句的信息也可能很有用。

Also , if you are working with objects, then it is possible to create a single DB connection object , and pass it to multiple other classes to use it: 同样,如果您使用的是对象,则可以创建单个DB连接对象,并将其传递给其他多个类以使用它:

$pdo = new PDO('sqlite::memory:');

$a = new Foo( $pdo );
$b = new Bar( $pdo, 'something');

This way you pass both objects the same database connection, and you do not need to reinitialize it. 这样,您就可以将两个对象传递给同一数据库连接,而无需重新初始化它。

Ok. 好。 You will want to keep the connection to the mysql database somewhere. 您将需要保持与mysql数据库的连接。 A common use is $conn. 常见用法是$ conn。

So you would have 所以你会

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to connect to database");

Then, either from the URL or Post, or just some variables you have sitting in your php file, you can query the database by putting the variables in the query itself. 然后,从URL或Post,或者只是位于php文件中的一些变量,就可以通过将变量放在查询本身中来查询数据库。 Also, here you can use $conn so that you have one place to connect to the database, in an include for example, and you won't have to make all of the connection string in each place you need to connect to the DB. 另外,在这里您可以使用$ conn,以便在一个位置(例如,在include中)连接数据库,而不必在需要连接到数据库的每个位置上都放置所有连接字符串。

$query = "SELECT * FROM `TABLE` WHERE `COLUMN` LIKE '%" . $varToCompare . "%'";
$result=mysql_query($query,$conn);

Above you are using a like. 上面你用的是赞。 You may want to just look at doing .. Where column=$var. 您可能只想看做..其中column = $ var。

Then you can use php to spin through the results into an array (for queries where would get multiple rows). 然后,您可以使用php将结果旋转到一个数组中(用于查询将获得多行的查询)。

I think you're looking for something like this: 我认为您正在寻找这样的东西:

$count = mysql_num_rows($result);

//if there is more then 1 record retrieved from the database
if($count > 0)
{
    //Do what ever you want to do here, which I think you want to be
    while ($row = mysql_fetch_assoc($result))
    {
        echo $row["Columnname1"];
        echo $row["Columnname2"];
        echo $row["Columnname3"];
    }
}

else
{
    echo "There are no matches for this specific value";
}

You can get the queried data by rows as an associated array using mysql_fetch_array(): 您可以使用mysql_fetch_array()将查询的数据作为关联数组按行获取:

$row = 0;
$data = mysql_query("SELECT name1,name2 FROM ....");
while(($result = mysql_fetch_array($data)) !== false)
{
    echo "row = $row, name1 = " . $result["name1"] . ", name2 = " . $result["name2"];
    $row ++;
}

... or as an objects using mysql_fetch_object(): ...或使用mysql_fetch_object()作为对象:

$row = 0;
$data = mysql_query("SELECT name1,name2 FROM ....");
while(($result = mysql_fetch_object($data)) !== false)
{
    echo "row = $row, name1 = $result->name1, name2 = $result->name2";
    $row ++;
}

I'm not too sure of what you want, but I can see one probable bug here: you're using LIKE in a way which means = : in order to have LIKE to behave like a like , you need some joker chars : 我不太确定自己想要什么,但是我在这里看到一个可能的错误:您正在使用LIKE ,即= :为了使LIKE表现得像like ,您需要一些小丑字符:

"SELECT * FROM `TABLE` WHERE `COLUMN` LIKE 'VAULE'" // This will return all rows where column='VAUL'
"SELECT * FROM `TABLE` WHERE `COLUMN` LIKE '%VAULE%'" // This will return all rows where column='%VAUL%' // This will return any row containing 'VAUL' in column
"SELECT * FROM `TABLE` WHERE `COLUMN` LIKE '%VAULE'" // This will return all rows where column='%VAUL' // this will  return all rows ending by VAUL. I guess you get it now :)

An to retrieve the actual results: 检索实际结果:

$query = "SELECT * FROM `TABLE` WHERE `COLUMN` LIKE '%VAULE%'";
$result=mysql_query($query);

while (false !== ($row = mysql_fetch_assoc($result))) {
    //here $row is an array containing all the data from your mysql row
}

Try to write the database connection in another page no need to use function and include that page in where ever you need. 尝试在另一个页面中编写数据库连接,而无需使用功能,并将该页面包括在您需要的任何位置。

ex: require_once 'dbConnect.php';

dbConnect.php consists: dbConnect.php包含:

<?php
$dbname = 'datadump_pwmgr';
$dbuser = 'datadump_pwmgr';
$dbpass = 'kzddim05xrgl';
$dbhost = 'localhost';
mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to connect to database");
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM